package com.carleasoft.mps.cms.business.train.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.business.process.bean.po.MyApprovalPageCarPo;
import com.carleasoft.mps.cms.business.train.bean.entity.*;
import com.carleasoft.mps.cms.business.train.bean.po.*;
import com.carleasoft.mps.cms.business.train.bean.vo.*;
import com.carleasoft.mps.cms.business.train.service.TrainCarService;
import com.carleasoft.mps.cms.business.train.service.TrainDutyService;
import com.carleasoft.mps.core.result.R;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 训练日常消耗
 * </p>
 *
 * @author fan
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/trainCar")
public class TrainCarController {

    @Autowired
    private TrainCarService trainCarService;
    @Autowired
    private TrainDutyService trainDutyService;

    /**************************用车*******************************/
    /**
     * 训练日常消耗-用车-增加
     */
    @PostMapping("/trainCarAdd")
    public R add(@RequestBody TrainCarAddOrEditPo trainCarAddOrEditPo) {
        return R.ok(trainCarService.add(trainCarAddOrEditPo));
    }

    /**
     * 训练日常消耗-用车-编辑
     */
    @PostMapping("/trainCarEdit")
    public R<Boolean> edit(@RequestBody TrainCarAddOrEditPo trainCarAddOrEditPo) {
        return R.ok(trainCarService.edit(trainCarAddOrEditPo));
    }

    /**
     * 训练日常消耗-用车-根据编码改状态
     */
    @PostMapping("/editStatus")
    public R<Boolean> editStatus(@RequestBody TrainCarAddOrEditPo trainCarAddOrEditPo) {
        return R.ok(trainCarService.editStatus(trainCarAddOrEditPo));
    }

    /**
     * 训练日常消耗-用车-删除
     */
    @PostMapping("/trainCarDelete")
    public R<Boolean> delete(@RequestBody TrainCar trainCar) {
        return R.ok(trainCarService.delete(trainCar));
    }

    /**
     * 训练日常消耗-用车-分页查询
     */
    @PostMapping("/trainCarPageQuery")
    public R<Page<TrainCarVo>> pageQuery(@RequestBody TrainCarPageQueryPo trainCarPageQueryPo) {
        return R.ok(trainCarService.pageQuery(trainCarPageQueryPo));
    }

    /**
     * 训练日常消耗-用车-详情
     */
    @PostMapping("/trainCarDetail")
    public R<TrainCarDetailVo> detail(@RequestBody TrainCar trainCar) {
        return R.ok(trainCarService.detail(trainCar));
    }

    /**
     * 训练日常消耗-用车-导入
     */
    @PostMapping("/trainCar/import")
    public R<Boolean> trainCarImport(@RequestBody List<TrainCar> trainCarList) {
        return R.ok(trainCarService.trainCarImport(trainCarList));
    }

    /**
     * 训练日常消耗-用车-模板下载
     */
    @PostMapping("/trainCar/download")
    public R<Boolean> trainCarDownload(HttpServletResponse response) {
        return R.ok(trainCarService.trainCarDownload(response));
    }

//    /**
//     * 导出用车
//     */
//    @PostMapping("/trainCar/importCar")
//    public R<Boolean> importCar(@RequestBody MyApprovalPageCarPo myApprovalPageCarPo, HttpServletResponse response) {
//        return R.ok(trainCarService.importCar(myApprovalPageCarPo,response));
//    }

    /**
     * 导出用车
     */
    @PostMapping("/trainCar/importCar")
    public R<Boolean> importCar(HttpServletResponse response,@RequestPart("startTime") String startTime) throws ParseException {
        Workbook workbook = trainCarService.importCarCount(response,startTime);
        // 获取workbook对象
        // 单sheet或多sheet 只需要更改此处即可

        // 判断数据
//        if (workbook == null) {
//            return "fail";
//        }
        // 设置excel的文件名称
        String excelName = "测试excel";
        // 重置响应对象
        response.reset();
        // 当前日期，用于导出文件名称
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]";
        // 指定下载的文件名--设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=" + "文件名" + ".xls");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 写出数据输出流到页面
        try {
            OutputStream output = response.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            workbook.write(bufferedOutPut);
            bufferedOutPut.flush();
            bufferedOutPut.close();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 训练日常消耗-用车-模板解析
     */
    @PostMapping("/trainCar/parse")
    public R<List<TrainCar>> trainCarParse(@RequestParam("file") MultipartFile file) {
        return R.ok(trainCarService.trainCarParse(file));
    }
    /**************************消耗*******************************/
    /**
     * 训练日常消耗-消耗-增加
     */
    @PostMapping("/consumeAdd")
    public R<Boolean> consumeAdd(@RequestBody ConsumeAddOrEditPo consumeAddOrEditPo) {
        return R.ok(trainCarService.consumeAdd(consumeAddOrEditPo));
    }

    /**
     * 训练日常消耗-消耗-编辑
     */
    @PostMapping("/consumeEdit")
    public R<Boolean> consumeEdit(@RequestBody ConsumeAddOrEditPo consumeAddOrEditPo) {
        return R.ok(trainCarService.consumeEdit(consumeAddOrEditPo));
    }

    /**
     * 训练日常消耗-消耗-删除
     */
    @PostMapping("/consumeDelete")
    public R<Boolean> consumeDelete(@RequestBody TrainConsume trainConsume) {
        return R.ok(trainCarService.consumeDelete(trainConsume));
    }

    /**
     * 训练日常消耗-消耗-分页查询
     */
    @PostMapping("/consumePageQuery")
    public R<Page<TrainConsumeVo>> consumePageQuery(@RequestBody TrainConsumePageQueryPo trainConsumePageQueryPo) {
        return R.ok(trainCarService.consumePageQuery(trainConsumePageQueryPo));
    }

    /**
     * 训练日常消耗-消耗-详情
     */
    @PostMapping("/consumeDetail")
    public R<TrainConsumeDetailVo> consumeDetail(@RequestBody TrainConsume trainConsume) {
        return R.ok(trainCarService.consumeDetail(trainConsume));
    }

    /**
     * 训练日常消耗-消耗-维护-添加
     */
    @PostMapping("/consumeMaintain/add")
    public R<Boolean> consumeMaintainAdd(@RequestBody TrainConsumeItemAddOrEdit trainConsumeItemAddOrEdit) {
        return R.ok(trainCarService.consumeMaintainAdd(trainConsumeItemAddOrEdit));
    }

    /**
     * 训练日常消耗-消耗-维护-编辑
     */
    @PostMapping("/consumeMaintain/edit")
    public R<Boolean> consumeMaintainEdit(@RequestBody TrainConsumeItemAddOrEdit trainConsumeItemAddOrEdit) {
        return R.ok(trainCarService.consumeMaintainEdit(trainConsumeItemAddOrEdit));
    }

    /**
     * 训练日常消耗-消耗-维护-批量编辑
     */
    @PostMapping("/consumeMaintain/batchEdit")
    public R<Boolean> consumeMaintainBatchEdit(@RequestBody TrainConsumeItemBatchEditPo trainConsumeItemBatchEditPo) {
        return R.ok(trainCarService.consumeMaintainBatchEdit(trainConsumeItemBatchEditPo));
    }

    /**
     * 训练日常消耗-消耗-维护-删除
     */
    @PostMapping("/consumeMaintain/delete")
    public R<Boolean> consumeMaintainDelete(@RequestBody TrainConsumeItem trainConsumeItem) {
        return R.ok(trainCarService.consumeMaintainDelete(trainConsumeItem));
    }

    /**
     * 训练日常消耗-消耗-维护-分页
     */
    @PostMapping("/consumeMaintain/pageQuery")
    public R<Page<TrainConsumeItemVo>> consumeMaintainPageQuery(@RequestBody TrainConsumeItemPageQueryPo trainConsume) {
        return R.ok(trainCarService.consumeMaintainPageQuery(trainConsume));
    }

    /**
     * 训练日常消耗-消耗-维护-列表(不用传分页的参数)
     */
    @PostMapping("/consumeMaintain/list")
    public R<List<TrainConsumeItemVo>> consumeMaintainList(@RequestBody TrainConsumeItemPageQueryPo trainConsume) {
        return R.ok(trainCarService.consumeMaintainList(trainConsume));
    }

    /**
     * 训练日常消耗-消耗-维护-详情
     */
    @PostMapping("/consumeMaintain/detail")
    public R<TrainConsumeItemDetailVo> consumeMaintainDetail(@RequestBody TrainConsumeItem trainConsumeItem) {
        return R.ok(trainCarService.consumeMaintainDetail(trainConsumeItem));
    }

    /**
     * 训练日常消耗-消耗-维护-导入
     */
    @PostMapping("/consumeMaintain/import")
    public R<Boolean> consumeMaintainImport(@RequestParam("examCode") String examCode, @RequestParam("file") MultipartFile file) {
        return R.ok(trainCarService.consumeMaintainImport(examCode, file));
    }

    /**
     * 训练日常消耗-消耗-维护-模板下载
     */
    @PostMapping("/consumeMaintain/download")
    public R<Boolean> consumeMaintainDownload(@RequestBody TrainConsume trainConsume, HttpServletResponse response) {
        return R.ok(trainCarService.consumeMaintainDownload(trainConsume, response));
    }

    /**
     * 训练日常消耗-消耗-维护-模板解析
     */
    @PostMapping("/consumeMaintain/parse")
    public R<List<TrainConsumeItem>> consumeMaintainParse(@RequestParam("file") MultipartFile file) {
        return R.ok(trainCarService.consumeMaintainParse(file));
    }

    /**
     * 训练日常消耗-消耗-维护-完成
     */
    @PostMapping("/consumeMaintain/finish")
    public R consumeMaintainFinish(TrainConsume trainConsume) {
        return R.ok(trainCarService.consumeMaintainFinish(trainConsume));
    }

    /**
     * 训练日常消耗-消耗-下载(维护的药品信息)
     */
    @PostMapping("/consumeMaintain/downloadExcelFile")
    public R<Boolean> downloadExcelFile(@RequestBody TrainConsume trainConsume, HttpServletResponse response) {
        return R.ok(trainCarService.downloadExcelFile(trainConsume, response));
    }

    /**************************执勤*******************************/

    /**
     * 训练日常消耗-执勤-增加
     */
    @PostMapping("/trainDutyAdd")
    public R<Boolean> trainDutyAdd(@RequestBody TrainDutyAddOrEditPo trainDutyAddOrEditPo) {
        return R.ok(trainCarService.trainDutyAdd(trainDutyAddOrEditPo));
    }

    /**
     * 训练日常消耗-执勤-编辑
     */
    @PostMapping("/trainDutyEdit")
    public R<Boolean> trainDutyEdit(@RequestBody TrainDutyAddOrEditPo trainDutyAddOrEditPo) {
        return R.ok(trainCarService.trainDutyEdit(trainDutyAddOrEditPo));
    }

    /**
     * 训练日常消耗-执勤-删除
     */
    @PostMapping("/trainDutyDelete")
    public R<Boolean> trainDutyDelete(@RequestBody TrainDuty trainDuty) {
        return R.ok(trainCarService.trainDutyDelete(trainDuty));
    }

    /**
     * 训练日常消耗-执勤-分页查询
     */
    @PostMapping("/trainDutyPageQuery")
    public R<Page<TrainDutyVo>> trainDutyPageQuery(@RequestBody TrainDutyPageQueryPo trainDutyPageQueryPo) {
        return R.ok(trainCarService.trainDutyPageQuery(trainDutyPageQueryPo));
    }

    /**
     * 训练日常消耗-执勤-list
     */
    @PostMapping("/door/trainDutyList")
    public R<List<TrainDuty>> doorTrainDutyList() {
        return R.ok(trainDutyService.list());
    }

    /**
     * 训练日常消耗-执勤-详情
     */
    @PostMapping("/trainDutyDetail")
    public R<TrainDutyDetailVo> trainDutyDetail(@RequestBody TrainDuty trainDuty) {
        return R.ok(trainCarService.trainDutyDetail(trainDuty));
    }

    /**
     * 训练日常消耗-执勤-导入
     */
    @PostMapping("/trainDuty/import")
    public R<Boolean> trainDutyImport(@RequestParam("file") MultipartFile file) {
        return R.ok(trainCarService.trainDutyImport(file));
    }

    /**
     * 训练日常消耗-执勤-模板下载
     */
    @PostMapping("/trainDuty/download")
    public R<Boolean> trainDutyDownload(HttpServletResponse response) {
        return R.ok(trainCarService.trainDutyDownload(response));
    }

    /**
     * 训练日常消耗-执勤-模板解析
     */
    @PostMapping("/trainDuty/parse")
    public R<List<TrainDuty>> trainDutyParse(@RequestParam("file") MultipartFile file) {
        return R.ok(trainCarService.trainDutyParse(file));
    }

    /**
     * 办公统计-报车用车
     */
    @PostMapping("/trainCar/statistics")
    public R<TrainCarStatisticsVo> trainCarStatistics() {
        return R.ok(trainCarService.trainCarStatistics());
    }

    /**
     * 网上办事-报车用车-用车类型统计
     */
    @PostMapping("/doThingsOnline/useStatistics")
    public R useStatistics(@RequestBody UseStatisticsPo useStatisticsPo) {
        return R.ok(trainCarService.useStatistics(useStatisticsPo));
    }

    /**
     * 网上办事-报车用车-年度用车分类统计
     */
    @PostMapping("/doThingsOnline/carOfYearStatistics")
    public R carOfYearStatistics() {
        return R.ok(trainCarService.carOfYearStatistics());
    }

    /**
     * 统计分析-消耗情况-单位查询
     */
    @PostMapping("/statisticalAnalysis/dept")
    public R statisticalAnalysisDept() {
        return R.ok(trainCarService.statisticalAnalysisDept());
    }

    /**
     * 统计分析-消耗情况
     */
    @PostMapping("/statisticalAnalysis/consumption")
    public R statisticalAnalysisConsumption(@RequestBody StatisticalAnalysisConsumptionPo statisticalAnalysisConsumptionPo) {
        return R.ok(trainCarService.statisticalAnalysisConsumption(statisticalAnalysisConsumptionPo));
    }

    /**
     * 导入报车用车
     *
     * @param
     * @return
     */
    @PostMapping("/carImportExcel")
    public R contentImportExcel(@RequestPart("file") MultipartFile file) {
        List<CarExcelVo> carExcelVos = trainCarService.contentImportExcel(file);
        return R.ok(carExcelVos);
    }


    /**
     * 弹药消耗情况
     *
     * @param consumeListPo
     * @return
     */
    @PostMapping("/consumeList")
    public R consumeList(@RequestBody ConsumeListPo consumeListPo) {
        List<ConsumeListVo> consumeListVos = trainCarService.consumeList(consumeListPo);
        return R.ok(consumeListVos);
    }

}

